//
//  LLVIPViewModel.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLVIPViewModel.h"
#import "LLMenuItemDataViewModel.h"
#import "LLGoodsBasicViewModel.h"
#import "LLMenuItem.h"
#import "LLMenuItemData.h"
#import "LLGoodsBasic.h"
#import "LLBannerViewModel.h"

#define MODULE_TYPE_SCROLL_FIGURE          @"vipScrollFigure"
#define MODULE_TYPE_ACTIVITY               @"vipActivity"
#define MODULE_TYPE_RECOMMEND              @"vipSiftCommendList"

#define MODULE_TYPE_LIST                    @[MODULE_TYPE_SCROLL_FIGURE, MODULE_TYPE_ACTIVITY, MODULE_TYPE_RECOMMEND]


@interface LLVIPViewModel ()
@property(nonatomic, strong, readwrite) NSArray *dataSource;

@property(nonatomic, strong, readwrite) NSArray *bannerDataSource;
@property(nonatomic, strong, readwrite) NSArray *activityDataSource;
@property(nonatomic, strong, readwrite) NSArray *recommendDataSource;

@property(nonatomic, strong, readwrite) RACCommand *loadMenusCommand;
@property(nonatomic, assign, readwrite) BOOL isLoading;
@end

@implementation LLVIPViewModel

- (void)initialize {
    @weakify(self)
    self.bannerDataSource = @[];
    self.activityDataSource = @[];
    self.recommendDataSource = @[];

    self.loadMenusCommand =
            [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {

                void (^nextBlock)(NSString *, LLMenuItemDataBag *, LLMenuItem *)
                        = ^(NSString *moduleType, LLMenuItemDataBag *bag, LLMenuItem *item) {
                            @strongify(self)
                            if ([moduleType isEqualToString:MODULE_TYPE_SCROLL_FIGURE]) {
                                LLBannerViewModel *viewModel = [[LLBannerViewModel alloc] init];
                                viewModel.imgs =
                                        [[bag.menuItemDatas.rac_sequence.signal
                                                map:^id(LLMenuItemData *menuItemData) {
                                                    return menuItemData.imgUrl;
                                                }] toArray];
                                viewModel.menuItemDataBag = bag;
                                self.bannerDataSource = @[viewModel];
                            } else {
                                self.activityDataSource =
                                        [[[bag menuItemDatas].rac_sequence.signal
                                                map:^id(LLMenuItemData *menuItemData) {
                                                    LLMenuItemDataViewModel *viewModel = [[LLMenuItemDataViewModel alloc] init];
                                                    viewModel.imageUrl = menuItemData.imgUrl;
                                                    viewModel.title = menuItemData.name;
                                                    return viewModel;
                                                }] toArray];
                            }
                        };

                void (^goodsBasicNextBlock)(LLGoodsBasicBag *, LLMenuItem *)
                        = ^(LLGoodsBasicBag *bag, LLMenuItem *item) {
                            @strongify(self)
                            self.recommendDataSource =
                                    [[bag.goodsBasicItems.rac_sequence.signal
                                            map:^id(LLGoodsBasic *goods) {
                                                LLGoodsBasicViewModel *vm = [[LLGoodsBasicViewModel alloc] init];
                                                vm.goodsBasic = goods;
                                                return vm;
                                            }] toArray];
                        };

                NSDictionary *params = @{@"type" : @"vipShop"};
                return [[[[[[LLHTTPSessionManager shareManager]
                        request:URL_CHANNEL_AC_LIST params:params resultClass:LLMenuItemBag.class]
                        flattenMap:^RACStream *(LLMenuItemBag *menuItemBag) {
                            return menuItemBag.menuItems.rac_sequence.signal;
                        }]
                        flattenMap:^RACStream *(LLMenuItem *item) {
                            if ([MODULE_TYPE_LIST containsObject:item.identifier]) {
                                NSDictionary *params = @{@"type" : item.identifier};
                                RACSignal *signal = nil;
                                if ([MODULE_TYPE_RECOMMEND isEqualToString:item.identifier]) {
                                    params = @{@"page" : @(1), @"rows" : @10};
                                    signal = [[LLHTTPSessionManager shareManager]
                                            request:URL_VIP_COMMEND_LIST
                                             params:params
                                        resultClass:[LLGoodsBasicBag class]];
                                } else {
                                    params = @{@"type" : item.identifier};
                                    signal = [[LLHTTPSessionManager shareManager]
                                            request:URL_CHANNEL_AC_ACTIVITY_LIST
                                             params:params
                                        resultClass:[LLMenuItemDataBag class]];
                                }
                                return [RACSignal combineLatest:@[
                                        [RACSignal return:item.identifier],
                                        signal,
                                        [RACSignal return:item]
                                ]];
                            }
                            return [RACSignal empty];
                        }]
                        doNext:^(RACTuple *tupe) {
                            if ([tupe.first isEqualToString:MODULE_TYPE_RECOMMEND]) {
                                goodsBasicNextBlock(tupe.second, tupe.third);
                            } else {
                                nextBlock(tupe.first, tupe.second, tupe.third);
                            }
                        }]
                        finally:^{
                            @strongify(self)
                            self.dataSource = @[self.bannerDataSource, self.activityDataSource, self.recommendDataSource];
                            self.isLoading = NO;
                        }];
            }];

    [self.loadMenusCommand.errors subscribeNext:^(id x) {
        @strongify(self)
        self.isLoading = YES;
    }];
}

@end
